import pandas as pd
import numpy as np
import os
import sys
import model_code.gen_data as gd
import model_code.rfg as rfg
import model_code.per_mea as pm
from tensorflow import keras

#source = sys.argv[1] #tongji/vital
sources = ["tongji","vital"]#针对模型
flag = sys.argv[1]
pre_times = [5,10,15]
ioh_time = "1"
ob_wins = [5,10,15]
#d_path = source + "/dynamic_normalization/" + ioh_time + "-bt.csv"
d_paths = ["tongji/dynamic_normalization/1-bt.csv","vital/dynamic_normalization/1-bt.csv"]
#s_path =  source + "/" + "static/test_case.csv"#测试数据
s_paths =  ["tongji/static/test_case.csv","vital/static/test_case.csv"]#测试数据
c_path = "config_bt.json"

i = 1
whether = "False"
for source in sources:
    for d_path,s_path in zip(d_paths,s_paths):
            for pre_time in pre_times:
                pre_time = int(pre_time)
                pre_time = pre_time / 5
                for ob_win in ob_wins:
                    ob_win = int(ob_win)
                    static, dynamic, label = gd.gen_data(source, d_path, c_path, s_path, pre_time, ob_win)
                    dynamic_dim = dynamic.reshape(dynamic.shape[0], dynamic.shape[1], dynamic.shape[2], 1)

                    # model_path = "models/" + source + "-" + str(pre_time) + "-" + ioh_time + "-" + str(ob_win) + ".h5" 
                    model_path = "/home/mount/chy/models_all/" + source + "-" + str(pre_time) + "-" + ioh_time + "-" + str(ob_win) + ".h5"
                    f = "test_result.txt"#文件名
                    if(str(flag)=="BT"):
                        model_path = "/home/mount/chy/models_all/" + source + "+BT-" + str(pre_time) + "-" + ioh_time + "-" + str(ob_win) + ".h5"
                        f = "test_result+BT.txt"#文件名
                        whether = "True"
                    if not os.path.exists(model_path):
                        print("model is not exist...")
                        os._exit(0)
                    model = keras.models.load_model(model_path, custom_objects={"AUC": pm.AUC})

                    y_pred = model.predict([dynamic_dim, dynamic])
                    y_true = label

                    #这里准备用文件存储
                    print("AUC: " + str(pm.AUC(y_true, y_pred)))
                    print("precision: " + str(pm.precision(y_true, y_pred)))
                    print("recall: " + str(pm.recall(y_true, y_pred)))
                    print("sensitivity: " + str(pm.TPR(y_true, y_pred)))
                    print("specificity: " + str(pm.TNR(y_true, y_pred)))  
                    print("F1: " + str(pm.F1(y_true, y_pred)))
                    print("accuracy: " + str(pm.accuracy(y_true, y_pred)))
                    print("count:"+str(i))#运行计数
                    #写文件
                    with open(f,"a") as file:   #”a"代表追加内容
                        file.write("model:"+model_path+"\n")
                        file.write("model source:"+source+" pre_time:"+str(pre_time*5)+" ioh_time:"+str(ioh_time)+" ob_win:"+str(ob_win)+"\n")
                        file.write("whether have BT?:"+whether+"\n")
                        file.write("d_path:"+d_path+"\n")
                        file.write("s_path:"+s_path+"\n")
                        file.write("AUC: " + str(pm.AUC(y_true, y_pred))+"\n")
                        file.write("precision: " + str(pm.precision(y_true, y_pred))+"\n")
                        file.write("recall: " + str(pm.recall(y_true, y_pred))+"\n")
                        file.write("sensitivity: " + str(pm.TPR(y_true, y_pred))+"\n")
                        file.write("specificity: " + str(pm.TNR(y_true, y_pred))+"\n")
                        file.write("F1: " + str(pm.F1(y_true, y_pred))+"\n")
                        file.write("accuracy: " + str(pm.accuracy(y_true, y_pred))+"\n\n")
                        file.write("-------------------------------------------------------------\n")
                    i+=1
